package com.saturn.sldb;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.saturn.app.db.DymaticCondition;
import com.saturn.app.db.ListData;
import com.saturn.app.db.ORMapping;
import com.saturn.app.db.ResultORMapping;
import com.saturn.app.db.SimpleDaoTemplate;

public class TempSub {
	private String id;
	private String identify;
	private String name;
	private String sex;
	private String contact;
	private String createTime;
	private String creater;
	private String createrName;
	private String createrDepartment;
	private String state;
	private String inputType;
	private String pid;
	private String personRelation;
	private String pname;

	/* private String userId; */

	private static ORMapping<TempSub> mapping = new ResultORMapping<TempSub>();
	private static Map<String, String> stateMap = new HashMap<String, String>();

	static {
		stateMap.put("创建", "审核");
		stateMap.put("审核", "核对");
		stateMap.put("核对", "初审");
		stateMap.put("初审", "抽查");
		stateMap.put("抽查", "决议");
		stateMap.put("决议", "通过");
	}

	public static int add(TempSub vo) {
		// 指定值对象类型(VOClass)。例子：User
		// 指定插入表名称(tableName)。例子：如user表3个列，tableName=user(id, name, gender)
		// 根据列的顺序获取值对象的属性值。例子：vo.getId(), vo.getName(), vo.getGender()
		return SimpleDaoTemplate
				.update("INSERT INTO sldb_tmp_sub("
						+ "identify, name, sex, "
						+ "contact, createTime, creater, "
						+ "createrName, createrDepartment, state, "
						+ "inputType, pid, personRelation, pname) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
						vo.identify,  vo.name, vo.sex,
						vo.contact, vo.createTime, vo.creater, vo.createrName,
						vo.createrDepartment, vo.state, vo.inputType, vo.pid,
						vo.personRelation, vo.pname);
	}

	public static int add(TempSub vo, Connection connection) {
		// 指定值对象类型(VOClass)。例子：User
		// 指定插入表名称(tableName)。例子：如user表3个列，tableName=user(id, name, gender)
		// 根据列的顺序获取值对象的属性值。例子：vo.getId(), vo.getName(), vo.getGender()
		if (connection != null) {
			return SimpleDaoTemplate
					.update(connection,
							"INSERT INTO sldb_tmp_sub("
									+ "identify, name, sex, "
									+ "contact, createTime, creater, "
									+ "createrName, createrDepartment, state, "
									+ "inputType, pid, personRelation, pname) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
							vo.identify, vo.name, vo.sex,
							vo.contact, vo.createTime, vo.creater,
							vo.createrName, vo.createrDepartment, vo.state,
							vo.inputType, vo.pid, vo.personRelation, vo.pname);
		} else {
			return SimpleDaoTemplate
					.update("INSERT INTO sldb_tmp_sub("
							+ "identify, healthCareType, name, sex, "
							+ "contact, createTime, creater, "
							+ "createrName, createrDepartment, state, "
							+ "inputType, pid, personRelation, pname,"
							+ " tempNumber, diseaseKind, diagnosisOrg) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
							vo.identify, vo.name, vo.sex,
							vo.contact, vo.createTime, vo.creater,
							vo.createrName, vo.createrDepartment, vo.state,
							vo.inputType, vo.pid, vo.personRelation, vo.pname);
		}

	}

	public static int edit(TempSub vo) {
		// 指定值对象类型(VOClass)。例子：User
		// 指定插入表名称(tableName)。例子：如user表3个列，tableName=user
		// 指定修改列信息(modify)。例子：name=?, value=?
		// 根据修改列的顺序获取值对象的属性值。例子：vo.getName(), vo.getValue(), vo.getId()
		return SimpleDaoTemplate.update("UPDATE sldb_tmp_sub SET "
				+ "pid,identify = ?, type = ?, name = ?, "
				+ "sex = ?, permanent = ?, nodeType = ?, "
				+ "home = ?, contact = ?, address = ?, "
				+ " createTime = ?, creater = ?, "
				+ "createrName = ?, state = ?, createrDepartment = ?, "
				+ "inputType = ?, personRelation = ?, pname = ? "
				+ "WHERE id = ?", vo.pid, vo.identify,
				vo.name, vo.sex, vo.contact, vo.createTime, vo.creater,
				vo.createrName, vo.createrDepartment, vo.state, vo.inputType,
				vo.pid, vo.personRelation, vo.pname, vo.id);
	}

	public static TempSub get(String id) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定表主键(key)。例子:key=id
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.queryOne(
				"SELECT * FROM sldb_tmp_sub WHERE 1 = 1 and id = '" + id + "'",
				null, mapping, TempSub.class);
	}

	public static List<TempSub> getByPid(String pid) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定表主键(key)。例子:key=id
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.query(
				"SELECT * FROM sldb_tmp_sub WHERE 1 = 1 and pid = '" + pid + "'",
				null, mapping, TempSub.class);
	}
	
	public static TempSub getIdWithTime(String createTime) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定表主键(key)。例子:key=id
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.queryOne(
				"SELECT id FROM sldb_TempSub WHERE 1 = 1 and createTime = '"
						+ createTime + "'", null, mapping, TempSub.class);
	}

	public static ListData<TempSub> getAll(TempSub vo, String start,
			String offset, String orderBy, String order) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.query(
				"SELECT * FROM sldb_tmp_sub WHERE 1 = 1",
				new DymaticCondition().addSimpleCondition(vo, "identify",
						"name", "sex", "type", "createrDepartment",
						"createTime", "state", "creater", "inputType")
						.addCondition("ORDER BY {0} {1}", orderBy, order),
				mapping, TempSub.class, start, offset);
	}

	public static ListData<TempSub> getAllWithOutCreater(TempSub vo,
			String start, String offset, String orderBy, String order) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.query(
				"SELECT * FROM sldb_tmp_sub WHERE 1 = 1",
				new DymaticCondition().addSimpleCondition(vo, "state",
						"identify", "name", "sex", "createrName", "createTime")
						.addCondition("ORDER BY {0} {1}", orderBy, order),
				mapping, TempSub.class, start, offset);
	}

	public static ListData<TempSub> getAllByPid(TempSub vo, String start,
			String offset, String orderBy, String order) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.query(
				"SELECT * FROM sldb_tmp_sub WHERE 1 = 1",
				new DymaticCondition()
						.addSimpleCondition(vo, "state", "identify", "name",
								"sex", "createrName", "createTime")
						.addCondition("and pid = '?' ", vo.getPid())
						.addCondition("ORDER BY {0} {1}", orderBy, order),
				mapping, TempSub.class, start, offset);
	}

	public static ListData<TempSub> getAllWithoutCreater(TempSub vo,
			String start, String offset, String orderBy, String order) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate.query(
				"SELECT * FROM sldb_TempSub WHERE 1 = 1",
				new DymaticCondition().addSimpleCondition(vo, "state",
						"identify", "name", "gender", "createrName",
						"createTime").addCondition("ORDER BY {0} {1}", orderBy,
						order), mapping, TempSub.class, start, offset);
	}

	public static int replay(String id, String state) {
		return SimpleDaoTemplate.update(
				"UPDATE sldb_TempSub SET state = ? where id = ?", state, id);
	}

	@SuppressWarnings("unused")
	private static int state(Connection connection, String id, String state) {
		return SimpleDaoTemplate.update(connection,
				"UPDATE sldb_TempSub SET state = ? WHERE id = ?", state, id);
	}

	public static int UpdateState(String id, String state) {
		return SimpleDaoTemplate.update(
				"UPDATE sldb_TempSub SET state = ? WHERE id = ?", state, id);
	}

	public static int remove(final String id) {
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		return SimpleDaoTemplate.update(
				"DELETE FROM sldb_tmp_sub WHERE id = ?", id);
	}

	public static void removes(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				remove(id);
			}
		}
	}

	public static int removeByPid(String pid, Connection connection) {
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		if(connection!=null){
			return SimpleDaoTemplate.update(connection,"DELETE FROM sldb_tmp_sub WHERE pid = ?",
					pid);
		}else{
			return SimpleDaoTemplate.update("DELETE FROM sldb_tmp_sub WHERE pid = ?",
					pid);
		}
		
	}
	
	public static ListData<TempSub> getAllOldWithOutUserId(TempSub vo,
			String start, String offset, String orderBy, String order) {
		// 指定值对象类型(VOClass)。例子VOClass=User
		// 指定插入表名称(tableName)。例子：如user表，tableName=user
		// 指定O-R映射规则对象。默认mapping
		return SimpleDaoTemplate
				.query("select distinct (s.pid), p.* from  sldb_TempSub p ,sldb_TempSub_state s where 1 = 1 and p.id = s.pid",
						new DymaticCondition(), mapping, TempSub.class, start,
						offset);
	}

	public TempSub(String id, String identify, 
			String name, String sex, String contact, String createTime,
			String creater, String createrName, String createrDepartment,
			String state, String inputType, String pid, String personRelation,
			String pname) {
		super();
		this.id = id;
		this.identify = identify;
		this.name = name;
		this.sex = sex;
		this.contact = contact;
		this.createTime = createTime;
		this.creater = creater;
		this.createrName = createrName;
		this.createrDepartment = createrDepartment;
		this.state = state;
		this.inputType = inputType;
		this.pid = pid;
		this.personRelation = personRelation;
		this.pname = pname;
	}

	public TempSub() {
		super();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIdentify() {
		return identify;
	}

	public void setIdentify(String identify) {
		this.identify = identify;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}

	public String getCreaterName() {
		return createrName;
	}

	public void setCreaterName(String createrName) {
		this.createrName = createrName;
	}

	public String getCreaterDepartment() {
		return createrDepartment;
	}

	public void setCreaterDepartment(String createrDepartment) {
		this.createrDepartment = createrDepartment;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getInputType() {
		return inputType;
	}

	public void setInputType(String inputType) {
		this.inputType = inputType;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPersonRelation() {
		return personRelation;
	}

	public void setPersonRelation(String personRelation) {
		this.personRelation = personRelation;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	@Override
	public String toString() {
		return new JSONObject(this).toString();
	}
}
